package com.winit.agent.repository;

import com.winit.agent.model.Agent;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

@Repository
public class AgentRepositoryExtImpl implements IAgentRepositoryExt {
    @PersistenceContext
    private EntityManager entityManager;

    @Transactional
    @Override
    public boolean updateByIdForPropertyNotNull(Agent agent) {
        String id = agent.getAgentId();
        if (StringUtils.isEmpty(id)){
            return false;
        }
        Agent entity = entityManager.find(Agent.class, id);
        if (entity == null){
            return false;
        }
        if (!StringUtils.isEmpty(agent.getAgentIp())){
            entity.setAgentIp(agent.getAgentIp());
        }
        if (!StringUtils.isEmpty(agent.getAgentPort())){
            entity.setAgentPort(agent.getAgentPort());
        }
        entityManager.merge(entity);
        entityManager.flush();
        return true ;
    }
}
